package com.example.hi;

import androidx.appcompat.app.AppCompatActivity;

import android.app.Activity;
import android.app.AlarmManager;
import android.app.PendingIntent;
import android.app.Service;
import android.content.Intent;
import android.content.SearchRecentSuggestionsProvider;
import android.content.SharedPreferences;
import android.os.Bundle;
import android.util.Log;
import android.view.View;
import android.widget.EditText;


public class MainActivity2 extends AppCompatActivity {
    public  final String TAG ="MainActivity2";

    EditText dollarText;
    EditText euroText;
    EditText wonText;
    private final static long ONE_DAY_MSECOND = 24 * 60 * 60 * 1000;
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main2);
        Intent intent=getIntent();
       float  dollar2=intent.getFloatExtra("dollar_rate_key",0.0f);
       float euro2=intent.getFloatExtra("euro_rate_key",0.0f);
       float  won2=intent.getFloatExtra("won_rate_key",0.0f);

        Intent i = new Intent();
        //创建闹钟对象
        AlarmManager aManager = (AlarmManager)getSystemService(Service.ALARM_SERVICE);
        PendingIntent pi = PendingIntent.getActivity(this, 0, i, PendingIntent.FLAG_UPDATE_CURRENT);
        // 设置闹钟为可将手机从休眠中唤醒，第一次开始时间为现在，间隔为一天，完成的任务为pendingIntent
        aManager.setRepeating(AlarmManager.RTC_WAKEUP, System.currentTimeMillis(), ONE_DAY_MSECOND, pi);


       Log.i(TAG,"onCreate: dollar2= " +dollar2);
       Log.i(TAG,"onCreate: euro2= " +euro2);
        Log.i(TAG,"onCreate: won2= " +won2);

        dollarText=(EditText)findViewById(R.id.dollarRate);
        euroText=(EditText)findViewById(R.id.euroRate);
        wonText=(EditText)findViewById(R.id.wonRate);

        //显示数据到控件

        dollarText.setText(String.valueOf(dollar2));
        euroText.setText(String.valueOf(euro2));
        wonText.setText(String.valueOf(won2));

    }
     public void save(View save){

        Log.i(TAG,"save");
        //获取新的值
         float newDollar = Float.parseFloat(dollarText.getText().toString());
         float newEuro = Float.parseFloat(euroText.getText().toString());
         float newWon = Float.parseFloat(wonText.getText().toString());

         Log.i(TAG,"save:获取到新的值");
         Log.i(TAG,"save: newDollar = " +newDollar );
         Log.i(TAG,"save: newEuro= " +newEuro);
         Log.i(TAG,"save: newWon = " +newWon );


         SharedPreferences sp = getSharedPreferences("myRate", Activity.MODE_PRIVATE);
         SharedPreferences.Editor editor = sp.edit();
         editor.putFloat("dollar_rate",newDollar);
         editor.putFloat("euro_rate",newEuro);
         editor.putFloat("won_rate",newWon);

         Log.d("Main2","saved"+newDollar);
         editor.apply();

         //保存到Bundle或放入到Extra
         Intent intent = getIntent();
         Bundle bd1 =new Bundle();
         bd1.putFloat("key_dollar",newDollar);
         bd1.putFloat("key_euro",newEuro);
         bd1.putFloat("key_won",newWon);
         intent.putExtras(bd1);
         setResult(2,intent);

         //返回到调用界面
         finish();
    }
}